///
/// @ProjectName:    learning_rust
/// @Description:    
/// @File:           jz5.rs
/// @Author:         wujehy
/// @Email:          wujehy@qq.com
/// @Time:           2021-07-01 21:23
///

use crate::error::GeeError;

struct Solution{
    stack1 : Vec<i32>,
    stack2 :Vec<i32>
}

impl Solution{
    fn new() -> Self{
        Solution{
            stack1: vec![],
            stack2: vec![]
        }
    }
    pub fn push(&mut self, val : i32){
        self.stack1.push(val);
    }
    #[allow(unused_must_use)]
    pub fn pop(&mut self) -> Result<i32,GeeError>{
        if self.stack1.is_empty() && self.stack2.is_empty() {
            return Err(GeeError::from("stack is empty")) ;
        }

        if self.stack2.is_empty() {
            while !self.stack1.is_empty() {
                self.stack1.pop().map(|data|{
                    self.stack2.push(data);
                });
            }
        }
        return Ok(self.stack2.pop().unwrap());
    }
}


#[test]
fn test5()
{
    let mut solution = Solution::new();
    solution.push(1);
    solution.push(2);
    solution.push(3);
    solution.push(4);
    solution.push(5);
    solution.push(6);

    let print_func = |data|{
        println!("data : {}" , data);
    };

    solution.pop().map(print_func).unwrap();
    solution.pop().map(print_func).unwrap();
    solution.pop().map(print_func).unwrap();
    solution.pop().map(print_func).unwrap();
    solution.pop().map(print_func).unwrap();
    solution.pop().map(print_func).unwrap();
    // solution.pop().map(print_func).expect("erorr"); // NOTE wujehy 越界测试
}